package net.office.utils;

import net.office.bean.WorkTable;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarBean {
    static Calendar cal = Calendar.getInstance();
    public static final int currentYear = cal.get(Calendar.YEAR);//当前是哪一年
    public static final int currentMonth = cal.get(Calendar.MONTH )+1;//当前是哪个月，注意这里的月是从0开始计数的，month+1即为当前月
    public static final int currentDay = cal.get(Calendar.DATE);

    String calendar = null;
    private int year;
    private int month;

    private List<WorkTable> workTableList;

    public List<WorkTable> getWorkTableList() {
        return workTableList;
    }
    public void setWorkTableList(List<WorkTable> workTableList) {
        this.workTableList = workTableList;
    }

    public void setYear(int year)
    {
        this.year = year;
    }
    public int getYear()
    {
        if(year==0){
            year=currentYear;
        }
        return year;
    }
    public void setMonth(int month)
    {
        this.month = month;
    }
    public int getMonth()
    {
        if(month==0){
            month=currentMonth;
        }
        return month;
    }
    public String getCalendar() {
        StringBuffer buffer = new StringBuffer();
        Calendar rili = Calendar.getInstance();
        if (year == 0 || month == 0) {
            year = currentYear;
            month = currentMonth;
        }
        rili.set(year, month - 1, 1);//將日历翻到year年month月1日，注意0表示一月，以此类推11表示12月
        int startDay = rili.get(Calendar.DAY_OF_WEEK) - 1;//获取1日是星期几(get方法返回的值是1表示星期日，返回的值是7表示星期六)
        int day = 0;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            day = 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            day = 30;
        }
        if (month == 2) {
            if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                day = 29;
            } else {
                day = 28;
            }
        }
        String a[] = new String[42];
        for (int i = 0; i < startDay; i++) {
            a[i] = "&nbsp;";
        }
        for (int i = startDay, n = 1; i < startDay + day; i++) {
            a[i] = String.valueOf(n);
            n++;
        }
        for (int i = startDay + day; i < 42; i++) {
            a[i] = "&nbsp;";
        }

        List<Date> signedList = new ArrayList<Date>();//值班日列表
        for(int z=0; z<workTableList.size(); z++){
            signedList.add(workTableList.get(z).getWorkDate());
        }
/*        signedList.add(strToDateLong("2018-11-11 13:51:11"));
        signedList.add(strToDateLong("2018-11-18 13:51:11"));
        signedList.add(strToDateLong("2018-11-08 13:51:11"));*/

        //用表格表示数組
        buffer.append("<table class=\"table table-bordered\" id=\"sign-table\">");
        buffer.append("<thead><tr>");
        String weekday[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        for (int k = 0; k < 7; k++) {
            buffer.append("<td>" + weekday[k] + "</td>");
        }
        buffer.append("</tr></thead><tbody>");

        for (int k = 0; k < 42; k = k + 7) {
            buffer.append("<tr>");
            for (int j = k; j < Math.min(7 + k, 42); j++) {
                Date date1 = strToDate(year + "-" + month + "-" + a[j]);
                /*System.out.println(date1);*/

                    if (a[j].equals(String.valueOf(currentDay)) && month == currentMonth) {//判断是否为当前日期
                        buffer.append("<td class=\"");
                        for (Date date2 : signedList) {
                            System.out.println(date2);
                            if (date1!=null && isSameDate(date1, date2)) {//判断是否为值班日期
                                buffer.append("red-bg");
                                break;
                            }
                        }
                        buffer.append(" color-yellow\">" + a[j] + "</td>");
                    }else {
                        buffer.append("<td class=\"");
                        for (Date date2 : signedList) {
                            System.out.println(date2);
                            if (date1!=null && isSameDate(date1, date2)) {
                                buffer.append("light-red-bg");
                                break;
                            }
                        }
                        buffer.append("\">" + a[j] + "</td>");
                    }

            }
            buffer.append("</tr>");
        }
        buffer.append("</tbody></table>");
        calendar = new String(buffer);
        return calendar;
    }

    public  String getStringDate(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }
    public Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    public String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }
    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    private boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }
}
